<?php
namespace app\service;

use think\Db;

class UserWarn
{
    public static function CheckWarn($user_id, $value)
    {
        $warn = Db::name('user_warn')->find($user_id);
        if($warn){
            Db::name('user_warn_records')->insert(['user_id'=>$user_id, 'desc'=>$value, 'time'=>time()]);
        }else{
            $userinfo = Db::name('user')->find($user_id);
            $userRepeatCheck = Db::name('user_repeat_check')->find($user_id);
            if($userinfo){
                if($userRepeatCheck['zhifucounts']){
                    $zhifucounts = explode(',', $userRepeatCheck['zhifucounts']);
                    if($userinfo['zhifucount']){
                        array_push($zhifucounts, $userinfo['zhifucount']);
                    }
                    $where['b.zhifucount'] = ['in', array_unique($zhifucounts)];
                }elseif($userinfo['zhifucount']){
                    $where['b.zhifucount'] = $userinfo['zhifucount'];
                }
                if($userinfo['phone']){
                    $where['b.zhifucount'] = $userinfo['phone'];
                }
                if($userRepeatCheck['device_ids']){
                    $device_ids = explode(',', $userRepeatCheck['device_ids']);
                    if($userinfo['device_id']){
                        array_push($device_ids, $userinfo['device_id']);
                    }
                    $where['b.device_id'] = ['in', array_unique($device_ids)];
                }elseif($userinfo['device_id']){
                    $where['b.device_id'] = $userinfo['device_id'];
                }
                $warn = Db::name('user_warn a')->join('user b', 'a.user_id=b.id')->whereOr($where)->find();
                if($warn){
                    Db::name('user_warn_records')->insert(['user_id'=>$user_id, 'desc'=>$value . '（来源于用户 '.$warn['id'].'）', 'time'=>time()]);
                }
            }
        }
    }
}
